package imp;

import dao.UserDao;
import model.Goods;
import model.User;
import util.DUBtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class User_imp implements UserDao {
    @Override
    public boolean registered(String id, String user_account, String user_name,
                              String user_password, String user_phone, String user_email, String user_imgurl) {
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        System.out.println(user_account + "___" + user_password);
        String sql = "insert into user(id,account,name,password,phone,email,imgurl,integral) values(?,?,?,?,?,?,?,?)";
        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, id);
            pstmt.setString(2, user_account);
            pstmt.setString(3, user_name);
            pstmt.setString(4, user_password);
            pstmt.setString(5, user_phone);
            pstmt.setString(6, user_email);
            pstmt.setString(7, user_imgurl);
            pstmt.setInt(8, 100);
            pstmt.executeUpdate();
            pstmt.close();
            con.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
    //修改信息
    @Override
    public Boolean UpdateUser(User user) {
        Connection connection = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        String sql = "update user set name=? , password=? , phone=? , email=? , imgurl=? where id=?";
        try {
            pstmt = connection.prepareStatement(sql);
            System.out.println("执行用户修改"+pstmt);
            pstmt.setString(1, user.getName());
            pstmt.setString(2, user.getPassword());
            pstmt.setString(3, user.getPhone());
            pstmt.setString(4, user.getEmail());
            pstmt.setString(5, user.getImgurl());
            pstmt.setString(6, user.getId());
            pstmt.executeUpdate();
            pstmt.close();
            connection.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public User GetSellerUser(String userid) {
        //        Date date = new Date();
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "select * from user where id=?";
        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, userid);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                User user = new User();
                user.setName(rs.getString("name"));
                user.setImgurl(rs.getString("imgurl"));
                return user;
            }
            rs.close();
            pstmt.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    @Override
    public User GetBuyerUser(String userid) {
        //        Date date = new Date();
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "select * from user where id=?";
        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, userid);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                User user = new User();
                user.setName(rs.getString("name"));
                user.setImgurl(rs.getString("imgurl"));
                return user;
            }
            rs.close();
            pstmt.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public User GetUserPhone(String userid) {
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "select * from user where id=?";
        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, userid);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                User user = new User();
                user.setPhone(rs.getString("phone"));
                return user;
            }
            rs.close();
            pstmt.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Boolean ReduceUserIntegral(String userid) {
        Connection connection = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        String sql = "update user set integral=integral-10 where id=?";
        try {
            pstmt = connection.prepareStatement(sql);
            System.out.println("用户积分扣20"+pstmt);
            pstmt.setString(1,userid);
            pstmt.executeUpdate();
            pstmt.close();
            connection.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public int GetUserIntegral(String userid) {
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "select * from user where id=?";
        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, userid);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                User user = new User();
                user.setIntegral(rs.getInt("integral"));
                return user.getIntegral();
            }
            rs.close();
            pstmt.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    //登录
    @Override
    public User login(String user_account, String user_password) {
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "select * from user where account=? and password=?";
        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, user_account);
            pstmt.setString(2, user_password);
            System.out.println(pstmt);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                User user = new User();
                user.setId(rs.getString("Id"));
                user.setAccount(rs.getString("account"));
                user.setName(rs.getString("name"));
                user.setPassword(rs.getString("password"));
                user.setPhone(rs.getString("phone"));
                user.setEmail(rs.getString("email"));
                user.setImgurl(rs.getString("imgurl"));
                user.setIntegral(rs.getInt("integral"));
                return user;
            }
            rs.close();
            pstmt.close();
            con.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }

        return null;
    }
}
